<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="SystemdPOST–Bootsequeue –bootloader（MBR）–kernel–rootfs-initcentos5 ：Sysvcentos6 ：upstartcentos7 ：systemd 新特性:系统引导时实现服务并行启动；按需激活进程系统状态快照基于依赖关系定义服务控制逻辑 核心概念 unit    由其相关配置文件进行标识、识别和配置；文件中主要包含了系统服务、监听的so">
<meta property="og:type" content="article">
<meta property="og:title" content="26.Systemd">
<meta property="og:url" content="http://yoursite.com/2020/06/26/26-Systemd/index.html">
<meta property="og:site_name" content="Lcxiao&#39;s blog">
<meta property="og:description" content="SystemdPOST–Bootsequeue –bootloader（MBR）–kernel–rootfs-initcentos5 ：Sysvcentos6 ：upstartcentos7 ：systemd 新特性:系统引导时实现服务并行启动；按需激活进程系统状态快照基于依赖关系定义服务控制逻辑 核心概念 unit    由其相关配置文件进行标识、识别和配置；文件中主要包含了系统服务、监听的so">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-26T23:32:05.000Z">
<meta property="article:modified_time" content="2020-06-27T07:32:17.716Z">
<meta property="article:author" content="lcxiao">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/06/26/26-Systemd/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>26.Systemd | Lcxiao's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="Lcxiao's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Lcxiao's blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-lifestyle">

    <a href="/lifestyle/" rel="section"><i class="fa fa-fw fa-lifestyle"></i>LifeStyle</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/06/26/26-Systemd/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="lcxiao">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lcxiao's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          26.Systemd
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-06-27 07:32:05 / 修改时间：15:32:17" itemprop="dateCreated datePublished" datetime="2020-06-27T07:32:05+08:00">2020-06-27</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Systemd<br>POST–Bootsequeue –bootloader（MBR）–kernel–rootfs-init<br>centos5 ：Sysv<br>centos6 ：upstart<br>centos7 ：systemd</p>
<p>新特性:<br>系统引导时实现服务并行启动；<br>按需激活进程<br>系统状态快照<br>基于依赖关系定义服务控制逻辑</p>
<p>核心概念 unit<br>    由其相关配置文件进行标识、识别和配置；文件中主要包含了系统服务、监听的socket、保存的快照以及其他与init相关的信息；<br>    这些配置文件主要保存在<br>    /etc/systemd<br>    /lib/systemd/system<br>    /usr/lib/systemd/system<br>    /run/systemd/syetem</p>
<p>unit的常见类型；<br>Service unit 文件扩展名是.service，用于定义系统服务<br>Target unit 文件扩展为.target 用于模拟实现“运行级别”<br>Device unit  .device 用于定义内核识别的设备<br>Mount unit .mount 定义文件系统挂载点 （用户组和名称空间）<br>Socket unit 。socket 用于标识进程间通信用到的socket文件；<br>Snapshot .snapshot 管理系统快照<br>Swap unit .swap 用于标识swap设备<br>Automount unit .automount 文件系统自动点设备<br>Path unit .path 用于定义文件系统中的文件或目录</p>
<p>关键特性：<br>    基于socket的激活机制：socket与程序分离<br>    基于bus的激活机制；<br>    基于device的激活机制；<br>    基于path的激活机制；<br>    系统快照：保存各unit的当前状态信息于持久存储设备中；实现回滚<br>    向后兼容SYSV的init脚本<br>        /etc/init.d<br>    不兼容：<br>        systemctl 的命令是固定不变的；<br>        非由systemd启动的服务，systemctl无法与之通信，无法控制此服务；（自己写个unit）</p>
<p>管理系统服务；<br>centos7 ：service类型的unit文件：<br> systemctl [OPTIONS…] COMMAND [NAME…]</p>
<p>启动、停止、重启、状态：<br>service NAME start  ===》 systemctl start NAME<br>stop<br>restart<br>status<br>    loaded：表示已装载，被管理<br>    Active：inactive（停止dead）（disabled）（不自启）<br>    Active: active (running）（运行中）</p>
<p>条件式重启：<br>    systemctl try-restart NAME</p>
<p>重载或重启服务：<br>systemctl reload-or-try-restart NAME<br>reload-or-restart<br>reload-or-try-restart</p>
<p>查看服务当前启动与否？<br>systemctl is-active NAME</p>
<p>查看所有已激活的服务；<br>systemctl list-units </p>
<p>systemctl list-units –type service<br>查看所有服务（已激活和未激活）<br>systemctl list-units -t service –all<br>chkconfig –list</p>
<p>开机自启<br>chkconfig NAME on<br>systemctl enable NAME</p>
<p>关闭自启<br>chkconfig NAME off<br>systemctl disable NAME</p>
<p>查看某服务是否能开机自启<br>systemctl is-enabled NAME<br>chkconfig –list NAME</p>
<p>禁止某服务设置为开机自启：<br>systemctl mask NAME</p>
<p>取消此限制<br>systemctl unmask NAME</p>
<p>查看服务的依赖关系：<br>systemctl list-dependencies NAME</p>
<p>管理target units：<br>运行级别：<br>0 == runlevel0.target poweroff.target<br> systemctl poweroff</p>
<p>1 == runlevel.target rescue.target</p>
<p>2 == runlevel2.target  multi-user.target<br>3 == runlevel3.target   multi-user.target<br>4 == runlevel4.target   multi-user.target</p>
<p>5 == runlevel5.target   graphical.target</p>
<p>6 == runlevel6.target   reboot.target</p>
<p>级别切换：<br>init N<br>systemctl isolate NAME.target</p>
<p>查看级别：<br>runlevel<br>systemctl list-units -t target</p>
<p>查看所有target<br>systemctl list-units -t target –all</p>
<p>获取默认运行级别<br>systemctl get-default</p>
<p>修改默认运行级别<br>systemctl set-default multi-user.target </p>
<p>sysv：修改inittab文件</p>
<p>切换至紧急救援模式：<br>systemctl rescue    会加载驱动以及1级别的</p>
<p>切换至紧急模式：<br>systemctl emergency    各种设置及驱动都不会，最简洁的模式</p>
<p>其他常见命令<br>    关机：systemctl halt ，systemctl poweroff<br>    重启：  systemctl reboot<br>    挂起：systemctl suspend<br>    快照：systemctl hibernate<br>    快照并挂起：systemctl hybrid-sleep、</p>
<p>SERVICE unit 文件组成<br>通常三部分组成<br>[Unit] ：定义与unit类型无关的通用选项；用于提供unit的描述信息，unit行为以及依赖关系等；<br>[Service] :与特定类型相关的专用选项；此处为service类型；<br>[Install] ：定义由systemctl enable 以及 systemctl disable 命令在实现服务启用或禁用时用到的一些选项；</p>
<p>Unit段的常用选项；<br>    Description：描述信息；意义性描述<br>    After：定义unit的启动次序；表示当前unit应该晚于哪些unit启动；其功能与Before相反；<br>    Requles ：依赖到的其他units，强依赖，被依赖的units无法激活时，当前unit无法激活<br>    Wants：依赖到的其他units；弱依赖；<br>    Conflicts：定义units之间的冲突关系；</p>
<p>Service段常用选项：<br>    TYPE:  定义影响ExecStart及相关参数的功能的unit进程启动类型；<br>        类型：<br>            simple：父进程和子进程（nginx之类的）<br>            forklng：后续进程启动之后这个进程会退出<br>            oneshot：<br>            dbus：后续进程只有在主进程得到<br>            notify：类似于simple<br>            idle：类似simple<br>    Environment: 定义环境配置文件，在ExecStart之前启动，并为之提供一些自定义选项；<br>    ExecStart ：指明启动unit要运行的命令或脚本；ExecStartPre、ExecStartPost<br>    ExecReload<br>    ExecStop ：指明停止unit要运行的命令或脚本<br>    Restart：意外终止。。</p>
<p>Install段：<br>    Alias：<br>    RequlredBy：被哪些unit所依赖<br>    WantedBy：被哪些units所依赖</p>
<p>注意：对于新创建的unit文件，或修改了的unit文件，必须要通知systemd重载此配置文件；<br>systemctl daemon-reload</p>
<p>练习：为当前系统的httpd服务提供一个unit文件</p>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/06/26/25-%E5%87%BD%E6%95%B0%E7%BC%96%E7%A8%8B/" rel="prev" title="25.函数编程">
      <i class="fa fa-chevron-left"></i> 25.函数编程
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/06/26/27-Selinux/" rel="next" title="27.Selinux">
      27.Selinux <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">lcxiao</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://163.com/" title="http:&#x2F;&#x2F;163.com" rel="noopener" target="_blank">163</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://baidu.com/" title="http:&#x2F;&#x2F;baidu.com" rel="noopener" target="_blank">baidu</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="/dalipan.com" title="dalipan.com">dalipan</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://google.com/" title="https:&#x2F;&#x2F;google.com" rel="noopener" target="_blank">Google</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lcxiao</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
